package com.wuliupai.delivery.wuliupaidelivery.activity;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.MissionDateInfo;
import com.wuliupai.delivery.wuliupaidelivery.bean.MissionStatisticsBean;
import com.wuliupai.delivery.wuliupaidelivery.constants.InterfaceNet;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.ui.ArcProgressBar;
import com.wuliupai.delivery.wuliupaidelivery.ui.ChartView;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static java.util.Collections.reverse;

/**
 * Descriptions：任务统计页面
 * <p>
 * Author：ChenME
 * Date：2017/2/10
 * Email：ibelieve1210@163.com
 */
@EActivity(R.layout.activity_mission_statistics)
public class MissionStatisticsActivity extends AppBaseActivity {

    @ViewById(R.id.arcProgressBar)
    ArcProgressBar arcProgressBar;
    @ViewById(R.id.chartView)
    ChartView chartView;

    private int baseKPI = 0;//基准KPI（元）
    private int currentKPI = 0;//已完成KPI（元）

    @AfterViews
    void onPageStart() {
        setDefaultTitle(this, R.string.string_missionStatistics_title);
        loadData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setArcProgress();
    }

    private void setArcProgress() {
        if (currentKPI > 0) {
            arcProgressBar.setMaxProgress(baseKPI);
            arcProgressBar.setCurrentValue(currentKPI);
        }
    }

    @Click(R.id.chartView)
    void startDetailPage() {
        MissionStatisticsDetailActivity_.intent(this).start();
        animUtils.leftInAnimation(this);
    }

    private void loadData() {
        MissionStatisticsBean requestBean = new MissionStatisticsBean();
        String requestJson = JsonUtils.createJsonString(requestBean);
        String requestAes = null;
        try {
            requestAes = JsonUtils.aes(requestJson);
            callNet(this, InterfaceNet.Net_GetMissionStatistics30Days, requestAes, new NetResultOperate() {
                @Override
                public void onSuccess(String content) {
                    Object obj = JsonUtils.parseJsonString(content, MissionStatisticsBean.class);
                    if (obj instanceof MissionStatisticsBean) {
                        MissionStatisticsBean bean = (MissionStatisticsBean) obj;
                        if (null != bean) {
                            List<MissionDateInfo> dateList = bean.getInfolist();


                            List<Float> moneyList = new ArrayList<>();
                            int moneyMax = 0;//最大的金额（分）

                            int dateListIndex = 0;
                            if (null != dateList) {
                                int dateListSize = dateList.size();
                                SimpleDateFormat mFormatterTime = new SimpleDateFormat("yyyy-MM-dd");
                                Date date = new Date(System.currentTimeMillis());
                                Calendar calendar = Calendar.getInstance();
                                for (int i = 0; i < 30; i++) {
                                    calendar.setTime(date);
                                    calendar.add(Calendar.DAY_OF_MONTH, 0 - i);
                                    String dateString = mFormatterTime.format(calendar.getTime());

                                    int moneyTemp = 0;
                                    if (dateListIndex < dateListSize) {
                                        MissionDateInfo dateInfoTemp = dateList.get(dateListIndex);
                                        if (dateString.equals(dateInfoTemp.date)) {
                                            moneyTemp = dateInfoTemp.money;
                                            dateListIndex++;
                                        }
                                        moneyMax = moneyMax > moneyTemp ? moneyMax : moneyTemp;
                                    }
                                    moneyList.add(moneyTemp / 100f);
                                }
                                reverse(moneyList);
                                setChartView(moneyMax / 100, moneyList);
                            }


                            baseKPI = bean.getStandardKPI() / 100;
                            currentKPI = bean.getActualKPI() / 100;
                            setArcProgress();
                        }
                    }
                }

                @Override
                public void onDataEmpty(String content) {

                }

                @Override
                public void onDataEnd(String content) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setChartView(int maxValue, List<Float> values) {
        chartView.setMaxValue(maxValue);
        chartView.setCurrentValue(values);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        animUtils.leftOutAnimation(this);
    }
}